<?php

class Home extends CI_Controller{

	function index()
	{
            $is_logged_in = $this->session->userdata('logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			//echo "YOU are Not Authorized";
			redirect('userprofile');
                        die();
		}
		/*$x='11';
		$this->load->model('Situation');
		$data['rows'] = $this->Situation->deleteSituation($x);*/
		
		/*$data = array(
			'name' => 'basem_feels_tired',
			'UserProfile_email' => 'mmm@d.c'
		);
		
		$this->load->model('Situation');
		$this->Situation->createSituation($data);*/
		
		$array1='';
		$html='';
		$user_email=$this->session->userdata('username');
		$this->load->model('Situation');
		$query=$this->Situation->getMySituations($user_email);
		$counter_situation_result=0;//to retrieve the first situation HTML
		foreach($query->result_array() as $row)
		{
			$sit_name=$row['name'];
			$sit_id=$row['id'];
			$sit_html=$row['html'];
                        $sit_context_count=$row['context_count'];
                        $sit_decs=$row['description'];//desc
                        $sit_privacy=$row['privacyType'];//privacy

			$sit=$sit_name.'#'.$sit_id;
			//echo $sit; to build the taps i.e. 'summer#1'
			if($counter_situation_result==0)
			{
				$array1=$sit;
				$counter_situation_result++;
				$curr_html=$sit_html;
                                $curr_context_count=$sit_context_count;
                                $curr_sit_name=$sit_name;
                                $curr_sit_desc=$sit_decs;
                                $curr_sit_id=$sit_id;
                                $curr_sit_privacy=$sit_privacy;
                                $counter_situation_result++;
                                $user_email=$this->session->userdata('username');
			}
			else
			{
				$array1=$array1.','.$sit;	
			}
		}
		$count_sit=$query->num_rows();
		//echo $array1;
		/*$array1 = "sasassa,wqwqwqw,hghghgh,sdskds,dsdkwiiw,wqwqwqw,hghghgh,sdskds,dsdkwiiw,wqwqwqw,hghghgh,sdskds,dsdkwiiw,wqwqwqw,hghghgh,sdskds,dsdkwiiw";*/
		//$count_sit= 6;
		//$json1=json_decode($array1,true);
                $data=array(
                    'situations'=>'',
                    'number_sit'=>0,
                    'html'=> '',
                    'context_count'=>0,
                    'curr_sit_name'=>'',
                    'curr_sit_id'=>0,
                    'curr_sit_desc'=>'',
                    'curr_sit_privacy'=>'',
                    'username' =>$user_email
                    );
                if($count_sit>0)//check result rows if exist fill the array
                {
                    $data = array(
			'situations'=>$array1,
			'number_sit'=>$count_sit,
			'html'=> $curr_html,
                        'context_count'=>$curr_context_count,
                        'curr_sit_name'=>$curr_sit_name,
                        'curr_sit_id'=>$curr_sit_id,
                        'curr_sit_desc'=>$curr_sit_desc,
                        'curr_sit_privacy'=>$curr_sit_privacy,
                        'username' =>$user_email
			);
                }
                
		
		$this->load->view('header');
		$this->load->view('editor',$data);
		//$this->create();
	
	}
	
	function saveSituation()
	{
                //echo 'dsdsdsdsd';
                $all_context_html=$_POST['contexts_html_string'];
                //echo $all_context_html;
                $param_rec_sit_id=$_POST['situation_id'];
                //echo $param_rec_sit_id;
		$param_rec_sit_html=$_POST['html'];
                //echo $param_rec_sit_html;
		$param_rec_situation_name=$_POST['situation_name'];
		$param_rec_situation_privacy=$_POST['situation_privavy'];
		$param_rec_situation_desc=$_POST['situation_desc'];
		$param_rec_count_context=$_POST['count_context'];
                $param_rec_height_ret=$_POST['height'];
                $param_rec_background_ret=$_POST['background'];
                $param_new_context_copy_name=$_POST['new_copy_name'];
		//$employer_id = $this->uri->segment(3);
		//echo $param_rec;
                // Define the errors.
                $json_errors = array(
                    JSON_ERROR_NONE => 'No error has occurred',
                    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
                    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
                    JSON_ERROR_SYNTAX => 'Syntax error',
                );

                if($param_rec_sit_id=='undefined')
                {
                    $context_html_array=explode('###',$all_context_html);
                    //sizeof($context_html_array);
                    $obj= json_decode($param_rec_sit_html);
                    //echo 'entered editiing for: '+$param_rec_sit_id;

                    //$obj = json_decode($param_rec_sit_html,true);
                    for($i=0;$i<sizeof($obj->contexts);$i++)
                    {
                        $obj->contexts[$i]->html=$context_html_array[$i];
                        $obj->contexts[$i]->flag='none';
                        //echo $context_html_array[$i];
                    }
                }
                else
                {
                    $context_html_array=explode('###',$all_context_html);

                    $obj= json_decode($param_rec_sit_html);
                    //echo 'entered editiing for: '+$param_rec_sit_id;
                    for($i=0;$i<sizeof($obj->contexts);$i++)
                    {
                        $obj->contexts[$i]->html=$context_html_array[$i];
                        //$obj->contexts[$i]->flag='none';
                        //echo $context_html_array[$i];
                    }
                }

                //foreach ($obj as $cont)
                    //echo $cont->title;
                //echo $obj->contexts[1]->html;
                //echo $json_errors[json_last_error()];
                //echo $param_rec_sit_html;
               // echo $obj;


                //echo print_r($param_rec_sit_html)."||".print_r($obj);


                $timstap;
                $user_email=$this->session->userdata('username');
		$data = array(
			'name' => $param_rec_situation_name,
			'UserProfile_email' => $user_email,
			'description' =>$param_rec_situation_desc,
			'privacyType'=>$param_rec_situation_privacy,
			'html' => $param_rec_height_ret,
                        'background_image'=>$param_rec_background_ret,
                        //'modificationDate'=>'',
			'context_count' => $param_rec_count_context
		);

                if($param_rec_sit_id=='undefined')
                {//create situation and its contexts
                    //create sit
                    //echo 'entered editiing for: '+$param_rec_sit_id;
                    $this->load->model('Situation');
                    $this->Situation->createSituation($data);
                    //get inserted id situation
                    $this->load->model('Context');
                    $last_id=mysql_insert_id();
                    for($i=0;$i<sizeof($obj->contexts);$i++)
                    {
                        $obj->contexts[$i]->situation_Id=$last_id;
                    }

                    //create its contexts
                    /*for($i=0;$i<$param_rec_count_context;$i++)
                    {

                        $this->Context->creatContext($obj->contexts[$i]);
                    }*/
                    for($i=0;$i<sizeof($obj->contexts);$i++)
                    {
                        //if($obj->contexts[$i]->flag=='new')
                            $obj->contexts[$i]->flag='none';
                            $this->Context->creatContext($obj->contexts[$i]);
                        /*if($obj->contexts[$i]->flag=='updated')
                            $this->Context->customizeContext($obj->contexts[$i]);
                        if($obj->contexts[$i]->flag=='deleted')
                            $this->Context->deleteContext($obj->contexts[$i]->id);*/
                    }
                    /*foreach ($obj->contexts as $conte)
                    {
                        //echo print_r($conte);
                        $this->Context->creatContext($conte);
                    }*/



                }
                else
                {//edit situation and its contexts
                    $this->load->model('Situation');
                    //echo 'entered editiing for: '+$param_rec_sit_id;
                    $this->Situation->editSituation($param_rec_sit_id,$data);
                    $this->load->model('Context');
                    //create its contexts
                    for($i=0;$i<sizeof($obj->contexts);$i++)
                    {
                        $cflag=explode("_",$obj->contexts[$i]->flag);
                        if($obj->contexts[$i]->flag=='cutted')
                        {
                            $obj->contexts[$i]->flag='none';
                           
                            $this->load->helper('date');
                            $this->load->library('simple_html_dom');
                            $mhtml=$obj->contexts[$i]->html;
                            $html=new simple_html_dom();
                            $html->load($mhtml);
                            $name=$obj->contexts[$i]->name;

                            $now = time();
                            $human = unix_to_human($now);
                            $unix = human_to_unix($human);

                            $obj->contexts[$i]->name=$name.'_'.$unix;

                            $html->find("div[id=o_$name]",0)->onmouseover="show('".$name.'_'.$unix."')";
                            $html->find("div[id=o_$name]",0)->onmouseout="hide('".$name.'_'.$unix."')";
                            $html->find("div[id=$name]",0)->ondblclick="createEditor('".$name.'_'.$unix."')";

                            $html->find("img[id=imgid_cut_$name]",0)->onmouseover="imgHover('imgid_cut_".$name.'_'.$unix."','cutHover.png')";
                            $html->find("img[id=imgid_cut_$name]",0)->onmouseout="imgHover('imgid_cut_".$name.'_'.$unix."','cut.png')";
                            $html->find("img[id=imgid_cut_$name]",0)->onclick="cut('".$name.'_'.$unix."')";
                            //need to change onclick with the new name :(

                            $html->find("img[id=imgid_del_$name]",0)->onmouseover="imgHover('imgid_del_".$name.'_'.$unix."','closeHover.png')";
                            $html->find("img[id=imgid_del_$name]",0)->onmouseout="imgHover('imgid_del_".$name.'_'.$unix."','download.png')";
                            $html->find("img[id=imgid_del_$name]",0)->onclick="DeletetContext('".$name.'_'.$unix."')";

                            $html->find("img[id=imgid_mod_$name]",0)->onmouseover="imgHover('imgid_mod_".$name.'_'.$unix."','modHover.png')";
                            $html->find("img[id=imgid_mod_$name]",0)->onmouseout="imgHover('imgid_mod_".$name.'_'.$unix."','mod.png')";
                            $html->find("img[id=imgid_mod_$name]",0)->onclick="edit_context('".$name.'_'.$unix."')";

                            $html->find("img[id=imgid_copy_$name]",0)->onmouseover="imgHover('imgid_copy_".$name.'_'.$unix."','copyHover.png')";
                            $html->find("img[id=imgid_copy_$name]",0)->onmouseout="imgHover('imgid_copy_".$name.'_'.$unix."','copyImg.png')";
                            $html->find("img[id=imgid_copy_$name]",0)->onclick="copy('".$name.'_'.$unix."')";

                            $html->find("img[id=imgid_cut_$name]",0)->id="imgid_cut_".$name.'_'.$unix;
                            $html->find("img[id=imgid_mod_$name]",0)->id="imgid_mod_".$name.'_'.$unix;
                            $html->find("img[id=imgid_del_$name]",0)->id="imgid_del_".$name.'_'.$unix;
                            $html->find("img[id=imgid_copy_$name]",0)->id="imgid_copy_".$name.'_'.$unix;

                            $html->find("p[id=p_$name]",0)->innertext=$name;
                            $html->find("p[id=p_$name]",0)->id="p_".$name.'_'.$unix;
                            $html->find("div[id=$name]",0)->id=$name.'_'.$unix;
                            $html->find("div[id=o_$name]",0)->id="o_".$name.'_'.$unix;
                            $obj->contexts[$i]->html=$html->save();

                            $this->Context->customizeContext($obj->contexts[$i]->id,$obj->contexts[$i]);
                        }
                        else if($obj->contexts[$i]->flag=='copy')
                        {
                            $temporary=$obj->contexts[$i]->situation_Id;//copy to sit id
                            $q=$this->Context->getSituation_id($obj->contexts[$i]->id);
                            foreach ($q->result_array() as $row)
                            {
                                $retreved_situation_id=$row['situation_Id'];
                            }
                            $obj->contexts[$i]->flag='none';
                            $obj->contexts[$i]->situation_Id=$retreved_situation_id;
                            $this->Context->customizeContext($obj->contexts[$i]->id,$obj->contexts[$i]);
                            $obj->contexts[$i]->situation_Id=$temporary;
                            $obj->contexts[$i]->id='';

                            $this->load->helper('date');
                            $this->load->library('simple_html_dom');
                            $mhtml=$obj->contexts[$i]->html;
                            $html=new simple_html_dom();
                            $html->load($mhtml);
                            $name=$obj->contexts[$i]->name;

                            $now = time();
                            $human = unix_to_human($now);
                            $unix = human_to_unix($human);

                            $obj->contexts[$i]->name=$param_new_context_copy_name.'_'.$unix;

                            $html->find("div[id=o_$name]",0)->onmouseover="show('".$param_new_context_copy_name.'_'.$unix."')";
                            $html->find("div[id=o_$name]",0)->onmouseout="hide('".$param_new_context_copy_name.'_'.$unix."')";
                            $html->find("div[id=$name]",0)->ondblclick="createEditor('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_cut_$name]",0)->onmouseover="imgHover('imgid_cut_".$param_new_context_copy_name.'_'.$unix."','cutHover.png')";
                            $html->find("img[id=imgid_cut_$name]",0)->onmouseout="imgHover('imgid_cut_".$param_new_context_copy_name.'_'.$unix."','cut.png')";
                            $html->find("img[id=imgid_cut_$name]",0)->onclick="cut('".$param_new_context_copy_name.'_'.$unix."')";
                            //need to change onclick with the new name :(

                            $html->find("img[id=imgid_del_$name]",0)->onmouseover="imgHover('imgid_del_".$param_new_context_copy_name.'_'.$unix."','closeHover.png')";
                            $html->find("img[id=imgid_del_$name]",0)->onmouseout="imgHover('imgid_del_".$param_new_context_copy_name.'_'.$unix."','download.png')";
                            $html->find("img[id=imgid_del_$name]",0)->onclick="DeletetContext('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_mod_$name]",0)->onmouseover="imgHover('imgid_mod_".$param_new_context_copy_name.'_'.$unix."','modHover.png')";
                            $html->find("img[id=imgid_mod_$name]",0)->onmouseout="imgHover('imgid_mod_".$param_new_context_copy_name.'_'.$unix."','mod.png')";
                            $html->find("img[id=imgid_mod_$name]",0)->onclick="edit_context('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_copy_$name]",0)->onmouseover="imgHover('imgid_copy_".$param_new_context_copy_name.'_'.$unix."','copyHover.png')";
                            $html->find("img[id=imgid_copy_$name]",0)->onmouseout="imgHover('imgid_copy_".$param_new_context_copy_name.'_'.$unix."','copyImg.png')";
                            $html->find("img[id=imgid_copy_$name]",0)->onclick="copy('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_cut_$name]",0)->id="imgid_cut_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("img[id=imgid_mod_$name]",0)->id="imgid_mod_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("img[id=imgid_del_$name]",0)->id="imgid_del_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("img[id=imgid_copy_$name]",0)->id="imgid_copy_".$param_new_context_copy_name.'_'.$unix;

                            $html->find("p[id=p_$name]",0)->innertext=$param_new_context_copy_name;
                            $html->find("p[id=p_$name]",0)->id="p_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("div[id=$name]",0)->id=$param_new_context_copy_name.'_'.$unix;
                            $html->find("div[id=o_$name]",0)->id="o_".$param_new_context_copy_name.'_'.$unix;
                            $obj->contexts[$i]->html=$html->save();

                            $this->Context->creatContext($obj->contexts[$i]);
                        }
                        else if($obj->contexts[$i]->flag=='newCopy')
                        {
                            $obj->contexts[$i]->flag='none';
                            //echo ($obj->contexts[$i]->id);
                            //$obj->contexts[$i]->situation_Id=$param_rec_sit_id;
                            $this->Context->creatContext($obj->contexts[$i]);
                            $obj->contexts[$i]->situation_Id=$param_rec_sit_id;


                            $this->load->helper('date');
                            $this->load->library('simple_html_dom');
                            $mhtml=$obj->contexts[$i]->html;
                            $html=new simple_html_dom();
                            $html->load($mhtml);
                            $name=$obj->contexts[$i]->name;

                            $now = time();
                            $human = unix_to_human($now);
                            $unix = human_to_unix($human);

                            $obj->contexts[$i]->name=$param_new_context_copy_name.'_'.$unix;

                            $html->find("div[id=o_$name]",0)->onmouseover="show('".$param_new_context_copy_name.'_'.$unix."')";
                            $html->find("div[id=o_$name]",0)->onmouseout="hide('".$param_new_context_copy_name.'_'.$unix."')";
                            $html->find("div[id=$name]",0)->ondblclick="createEditor('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_cut_$name]",0)->onmouseover="imgHover('imgid_cut_".$param_new_context_copy_name.'_'.$unix."','cutHover.png')";
                            $html->find("img[id=imgid_cut_$name]",0)->onmouseout="imgHover('imgid_cut_".$param_new_context_copy_name.'_'.$unix."','cut.png')";
                            $html->find("img[id=imgid_cut_$name]",0)->onclick="cut('".$param_new_context_copy_name.'_'.$unix."')";
                            //need to change onclick with the new name :(

                            $html->find("img[id=imgid_del_$name]",0)->onmouseover="imgHover('imgid_del_".$param_new_context_copy_name.'_'.$unix."','closeHover.png')";
                            $html->find("img[id=imgid_del_$name]",0)->onmouseout="imgHover('imgid_del_".$param_new_context_copy_name.'_'.$unix."','download.png')";
                            $html->find("img[id=imgid_del_$name]",0)->onclick="DeletetContext('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_mod_$name]",0)->onmouseover="imgHover('imgid_mod_".$param_new_context_copy_name.'_'.$unix."','modHover.png')";
                            $html->find("img[id=imgid_mod_$name]",0)->onmouseout="imgHover('imgid_mod_".$param_new_context_copy_name.'_'.$unix."','mod.png')";
                            $html->find("img[id=imgid_mod_$name]",0)->onclick="edit_context('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_copy_$name]",0)->onmouseover="imgHover('imgid_copy_".$param_new_context_copy_name.'_'.$unix."','copyHover.png')";
                            $html->find("img[id=imgid_copy_$name]",0)->onmouseout="imgHover('imgid_copy_".$param_new_context_copy_name.'_'.$unix."','copyImg.png')";
                            $html->find("img[id=imgid_copy_$name]",0)->onclick="copy('".$param_new_context_copy_name.'_'.$unix."')";

                            $html->find("img[id=imgid_cut_$name]",0)->id="imgid_cut_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("img[id=imgid_mod_$name]",0)->id="imgid_mod_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("img[id=imgid_del_$name]",0)->id="imgid_del_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("img[id=imgid_copy_$name]",0)->id="imgid_copy_".$param_new_context_copy_name.'_'.$unix;

                            $html->find("p[id=p_$name]",0)->innertext=$param_new_context_copy_name;
                            $html->find("p[id=p_$name]",0)->id="p_".$param_new_context_copy_name.'_'.$unix;
                            $html->find("div[id=$name]",0)->id=$param_new_context_copy_name.'_'.$unix;
                            $html->find("div[id=o_$name]",0)->id="o_".$param_new_context_copy_name.'_'.$unix;
                            $obj->contexts[$i]->html=$html->save();



                            $this->Context->creatContext($obj->contexts[$i]);
                            //echo $obj->contexts[$i]->name;
                        }
                        else if($obj->contexts[$i]->flag=='new')
                        {
                            $obj->contexts[$i]->flag='none';
                            $obj->contexts[$i]->situation_Id=$param_rec_sit_id;
                            $this->Context->creatContext($obj->contexts[$i]);
                            //echo $obj->contexts[$i]->name;
                        }
                        else if($obj->contexts[$i]->flag=='newCutted')
                        {
                            $obj->contexts[$i]->flag='none';
                            //echo ($obj->contexts[$i]->id);
                            //$obj->contexts[$i]->situation_Id=$param_rec_sit_id;
                            $this->Context->creatContext($obj->contexts[$i]);
                            //echo $obj->contexts[$i]->name;
                        }
                        else if($obj->contexts[$i]->flag=='updated'||$obj->contexts[$i]->flag=='none')
                        {
                            $obj->contexts[$i]->flag='none';
                            $this->Context->customizeContext($obj->contexts[$i]->id,$obj->contexts[$i]);
                            //echo $obj->contexts[$i]->name;
                        }
                        else if($obj->contexts[$i]->flag=='deleted')
                        {
                            $this->Context->deleteContext($obj->contexts[$i]->id);
                            //echo $obj->contexts[$i]->name;
                        }
                        else if($obj->contexts[$i]->flag == 'duplicate' )
                        {
                            $temporary=$obj->contexts[$i]->situation_Id;//copy to sit id
                            $q=$this->Context->getSituation_id($obj->contexts[$i]->id);
                            foreach ($q->result_array() as $row)
                            {
                                $retreved_situation_id=$row['situation_Id'];
                            }
                            $obj->contexts[$i]->flag='none';
                            $obj->contexts[$i]->situation_Id=$retreved_situation_id;
                            $this->Context->customizeContext($obj->contexts[$i]->id,$obj->contexts[$i]);
                            //The new Duplicate context
                            $obj->contexts[$i]->situation_Id=$temporary;
                            $tid=$obj->contexts[$i]->id;

                            $obj->contexts[$i]->flag='duplicate_'.$obj->contexts[$i]->id;
                            $obj->contexts[$i]->id='';
                            $this->load->library('simple_html_dom');
                            $mhtml=$obj->contexts[$i]->html;
                            $html=new simple_html_dom();
                            $html->load($mhtml);
                            $name=$obj->contexts[$i]->name;
                            $html->find("div[id=$name]",0)->outertext='';
                            //echo $e[0] ;
                            //$e=$e[0];
                            //$e->;

                            //$name="o_".$name;
                            //$html->find("div[id=$name]",0)->setAttribute("onload" , "loadDuplicate($tid);");
                            //$e=$e[0];
                            //$e;
                            //echo print_r($e);
                            //echo $html->save();
                            $this->load->helper('date');
                            $now = time();
                            $human = unix_to_human($now);
                            $unix = human_to_unix($human);
                            $html->find("p[id=p_$name]",0)->innertext=$param_new_context_copy_name;
                            $html->find("p[id=p_$name]",0)->id="p_".$param_new_context_copy_name.'_'.$unix;

                            $name=$param_new_context_copy_name.'_'.$unix;

                            $obj->contexts[$i]->html=$html->save();

                            $newHTML= str_replace($obj->contexts[$i]->name, $name, $obj->contexts[$i]->html);
                            //$newHTML= str_replace($obj->contexts[$i]->name, $param_new_context_copy_name, $newHTML);
                            $obj->contexts[$i]->html=$newHTML;
                            $obj->contexts[$i]->name=$name;
                            $this->Context->creatContext($obj->contexts[$i]);

                        }
                        else if($cflag[0] == 'duplicate'){
                            $this->load->library('simple_html_dom');
                            $mhtml=$obj->contexts[$i]->html;
                            $html=new simple_html_dom();
                            $html->load($mhtml);
                            $name=$obj->contexts[$i]->name;
                            $html->find("div[id=$name]",0)->outertext='';
                            $obj->contexts[$i]->html=$html->save();
                            $this->Context->customizeContext($obj->contexts[$i]->id,$obj->contexts[$i]);
                        }


                    }
                }
		//to update a row
		//$sid='6';
		//$this->Situation->editSituation($sid,$data);



		/*
		$name = $this->input->post('name');
		$data['main_content'] = 'contact_submitted';
		if ($this->input->post('ajax'))
		{
			$this->load->view($data['main_content']);
		}
		else
		{
			$this->load->view('includes/template', $data);
		}
		*/
	}
        function saveAsPdf($id)
	{
            $this->load->helper('to_pdf');
            // page info here, db calls, etc.
            $html = $this->getHTML($id); //some html code
            //$html = '<div id="Div1" ><div id="o_salem_0" ><div id="salem_0"  ><p>dcsd</p></div></div>';
            $this->load->model("Situation");
            $sitInfo=$this->Situation->getSitInfo($id);
            $sitName=$sitInfo->row()->name;

            pdf_create($html, $sitName);
	}
        function saveAsHtml($id)
	{
		$this->load->helper('download');
		$this->load->library('zip');
		$this->load->library('simple_html_dom');
		$str = $this->getHTML($id); //html
                $html = str_get_html($str);

		foreach($html->find('img') as $element)
		{
		//saving the image into an assosiation array
                    $orig_image_name = $element->src;
                    $index = strrpos($orig_image_name, "/");
                    $new_image_name = "images/" . substr($orig_image_name, $index+1);
                    $data[$new_image_name] = file_get_contents($orig_image_name);
                    $element->src = $new_image_name;
		}
                $ele=$html->find('script',1);
                //$style=$element->innertext;

                //echo print_r($ele);
                $htm_str=$ele->innertext;
                $back_imge_pos_start = strpos($htm_str, "url(")+4;
                $back_imge_pos_end = strpos($htm_str, ")");
                $orginal_link=substr($htm_str, $back_imge_pos_start,($back_imge_pos_end-$back_imge_pos_start));
                //echo $orginal_link;
                $index = strrpos($orginal_link, "/");
                $new_image_name = "images/" . substr($orginal_link, $index+1);
                $data[$new_image_name] = file_get_contents($orginal_link);
                $htm_str=str_replace($orginal_link,$new_image_name ,$htm_str );
                //echo $new_image_name;
                //$element->innertext=$style;

                $ele->innertext = $htm_str;
                //echo $ele->innertext;

                $this->load->model("Situation");
                $sitInfo=$this->Situation->getSitInfo($id);
                $sitName=$sitInfo->row()->name;

                $this->zip->add_data($data);
                $this->zip->add_data("$sitName.html",$html->save());
		//$data[] = ;

		$this->zip->download("$sitName.zip");
	}
        function getHTML($param_rec_sit)
	{
                //$param_rec_sit='374';//$_POST['situation_id_to_retrieve_html'];
		$this->load->model('Situation');
		$query=$this->Situation->getMySituation_html($param_rec_sit);
                $sit_data=array();
		foreach ($query->result_array() as $row)
		{
			$sit_name=$row['name'];
			$sit_id=$row['id'];
			$height=$row['html'];
                        $sit_context_count=$row['context_count'];
                        $sit_decs=$row['description'];//desc
                        $sit_privacy=$row['privacyType'];//privacy
                        $sit_background_image=$row['background_image'];
                        $sit_data = array(
                            'name' => $sit_name,
                            'id' => $sit_id,
                            'UserProfile_email' => 'mmm@d.c',
                            'description' =>$sit_decs,
                            'privacyType'=>$sit_privacy,
                            'context_count' => $sit_context_count
                        );
		}
		//$html='salem alkzhdsini';

                $this->load->model('Context');
		$query=$this->Context->getMyContext($param_rec_sit);
                $array_of_context=array();
                $all_html_contxs='';
                foreach ($query->result_array() as $row)
		{
			$name=$row['name'];
			$tags=$row['tags'];
			$html=$row['html'];
                        $color=$row['color'];
                        $id=$row['id'];
                        $sit_id=$row['situation_Id'];
                        $flag=$row['flag'];

                        $data = array(
                            'id'=> $id,
                            //'html'=> $html,
                            'tags'=> $tags,
                            'name'=> $name,
                            'color'=> $color,
                            'flag'=> $flag,
                            'situation_id'=> $sit_id
			);
                        array_push($array_of_context,$data);
                        $all_html_contxs=$all_html_contxs.$html;
		}
                $context_json=json_encode($array_of_context);
                $temporary_context=array(
                  'contexts'=>  $context_json,
                  'html'=> $all_html_contxs,
                  'username'=> $this->session->userdata('username'),
                  'situation_id' => $param_rec_sit,
                  'back_img'=>$sit_background_image,
                  'height'=>$height
                );
                return $this->load->view('viewpage',$temporary_context,true);

        }
        function getSituation()
	{
                $array_of_context=array();
		$param_rec_sit=$_POST['situation_id_to_retrieve_html'];
                if($param_rec_sit!=0)
		{
                    $this->load->model('Situation');
                    $query=$this->Situation->getMySituation_html($param_rec_sit);
                    $sit_data=array();
                    foreach ($query->result_array() as $row)
                    {
                            $sit_name=$row['name'];
                            $sit_id=$row['id'];
                            $html=$row['html'];
                            $sit_context_count=$row['context_count'];
                            $sit_decs=$row['description'];//desc
                            $sit_privacy=$row['privacyType'];//privacy
                            $sit_background_image=$row['background_image'];
                            $sit_data = array(
                                'name' => $sit_name,
                                'id' => $sit_id,
                                'UserProfile_email' => 'mmm@d.c',
                                'description' =>$sit_decs,
                                'privacyType'=>$sit_privacy,
                                'html'=>$html,
                                'sit_background_image'=>$sit_background_image,
                                'context_count' => $sit_context_count
                            );
                    }
                    //$html='salem alkzhdsini';

                    $this->load->model('Context');
                    $query=$this->Context->getMyContext($param_rec_sit);
                    $array_of_context=array();
                    foreach ($query->result_array() as $row)
                    {
                            $name=$row['name'];
                            $tags=$row['tags'];
                            $html=$row['html'];
                            $color=$row['color'];
                            $id=$row['id'];
                            $sit_id=$row['situation_Id'];
                            $flag=$row['flag'];

                            $cflag=explode ( "_" , $flag );

                            if($cflag[0]=="duplicate"){
                                $to_load_id=$cflag[1];
                                //echo $to_load_id;
                                $quer=$this->Context->getContext($to_load_id);
                                //echo $dcontext;
                                if($quer->num_rows()> 0){
                                    $dcontext=$quer->row();
                                    $dhtmlt=$dcontext->html;
                                    $this->load->library('simple_html_dom');
                                    $dhtml=new simple_html_dom();
                                    $dhtml->load($dhtmlt);
                                    //The current duplicated context
                                    $complete_html=new simple_html_dom();
                                    $complete_html->load($html);
                                    //name of outer of current
                                    $cname="o_".$name;
                                    //name of inner of original
                                    $dvname=$dcontext->name;
                                    $ec=$complete_html->find("div[id=$cname]",0);
                                    $ed=$dhtml->find("div[id=$dvname]",0);
                                    //echo $dvname;
                                    $newHTML= str_replace($dvname, $name, $ed->outertext);
                                    $ec->innertext=$newHTML . $ec->innertext;
                                    $html=$complete_html->save();
                                }
                                else{
                                    $this->Context->deleteContext($id);
                                    $cflag[0]!="del";
                                }
                                    
                                    
                            }

                            $data = array(
                                'id'=> $id,
                                'html'=> $html,
                                'tags'=> $tags,
                                'name'=> $name,
                                'color'=> $color,
                                'flag'=> $flag,
                                'situation_id'=> $sit_id
                            );
                            if($cflag[0]!="del")
                                array_push($array_of_context,$data);
                    }
                    $sit_json=json_encode($sit_data);
                    $context_json=json_encode($array_of_context);
                    echo $context_json."###".$sit_json;
                }
                else
                {
                    $sit_data = array(
                                'name' => '',
                                'id' => '',
                                'UserProfile_email' => 'mmm@d.c',
                                'description' =>'',
                                'privacyType'=>'',
                                'html'=>'',
                                'sit_background_image'=>'',
                                'context_count' => ''
                            );
                    $sit_json=json_encode($sit_data);
                    $context_json=json_encode($array_of_context);
                    echo $context_json."###".$sit_json;
                }
		/*$data = array(
			'html'=> $html,
                        'sit_name'=> $sit_name,
                        'sit_id'=> $sit_id,
                        'sit_context_count'=> $sit_context_count,
                        'sit_decs'=> $sit_decs,
                        'sit_privacy'=> $sit_privacy
			);
                $json=json_encode($data);
		echo $json;*/

		//to update a row
		//$sid='6';
		//$this->Situation->editSituation($sid,$data);



		/*
		$name = $this->input->post('name');
		$data['main_content'] = 'contact_submitted';
		if ($this->input->post('ajax'))
		{
			$this->load->view($data['main_content']);
		}
		else
		{
			$this->load->view('includes/template', $data);
		}
		*/
	}

        function getUserSituations()
            {
                $user_email=$_POST['userEmail'];
                $this->load->model('Situation');
                $this->load->model('Context');
                $query=$this->Situation-> getAllSituationsInfo($user_email);

                $sitArray=array();
                foreach ($query->result_array() as $row)
		{
			$id=$row['id'];
                        $q=$this->Context->getMyContext($id);
                        $count_context=$q->num_rows();
                        $name=$row['name'];
			$privacyType=$row['privacyType'];
			$description=$row['description'];

                        $data = array(
                            'id'=> $id,
                            'name'=> $name,
                            'privacyType'=> $privacyType,
                            'contextNo'=>$count_context,
                            'description'=> $description
			);
                        array_push($sitArray,$data);
		}
                $sit_json=json_encode($sitArray);
		echo $sit_json;

            }

           function getAll()//copy dialog
            {
                $allSitInfo='';
		$html='';
		$user_email=$_POST['userEmail'];
		$this->load->model('Situation');
		$query=$this->Situation-> getMySituationsAll($user_email);
		$counter_situation_result=0;//to retrieve the first situation HTML
		foreach ($query->result_array() as $row)
		{
			$sit_name=$row['name'];
			$sit_id=$row['id'];
			//$sit_html=$row['html'];
                        $sit_context_count=$row['context_count'];
                        $sit_decs=$row['description'];//desc
                        $sit_privacy=$row['privacyType'];//privacy

			$sit=$sit_name.'#'.$sit_id;

                        if($counter_situation_result==0)
			{

                            $allSitInfo=$sit;
                            //echo $allSitInfo;
                            $counter_situation_result++;
                        }
                        else
			{
				$allSitInfo=$allSitInfo.','.$sit;
				$counter_situation_result++;
			}
                        //if($counter_situation_result!=0)
                            //$allSitInfo=$allSitInfo.','.$sit;
			//$counter_situation_result++;
            }

            echo $allSitInfo;


            }

            function deleteSituation()
            {
                $sid=$_POST['situation_id_to_be_deleted'];
                $this->load->model('Situation');
                $this->Situation->deleteSituation($sid);

            }

            function getSuggestions()
            {

                $sit_name=$_POST['sit_name'];
                $this->load->model('Situation');
                //$this->load->view('');
                $query=$this->Situation->getSuggestion($sit_name);

 			//	print_r($query->result());
			//                 $sit_array= array();
			//             //  print_r($query);
			//                // echo $sit_name;
			//
			//    			    if($query->num_rows() == 0)
			//                 {
			//                     echo 'false';
			//
			//                 }
			//                 foreach($query->result_array() as $row)
			//                 {
			//                     $id=$row['id'];
			//                     $name=$row['name'];
			//                     $description=$row['description'];
			//
			//                     $data = array(
			//                             'id'=> $id,
			//                             'name'=> $name,
			//                             'description'=> $description
			// );
			//
			//
			//                 }
			//                 print_r($sit_array);

			$sit_array = $query->result();
                	$sit_json=json_encode($sit_array);
                echo $sit_json;

            }

            function editSituation()
        {

            $sitName=$_POST['sitName'];
            $sitPraivacy=$_POST['sitPraivacy'];
            $sitDesciption=$_POST['sitDesciption'];
            $sitId=$_POST['sitId'];
            $back_img=$_POST['back_img'];
            $data= array (
                'id'=>$sitId,
                'name'=>$sitName,
                'description'=>$sitDesciption,
                'privacyType'=>$sitPraivacy,
                'background_image'=>$back_img
            );
		$this->load->model('Situation');
		$query=$this->Situation->editSituation($sitId,$data);
                //echo $query;
               if($query!=null )
               {
                  echo 1; //true
               }
               else
               {
                   echo 0;//false
               }
        }
        function updateContext(){
            $cid=$_POST["cid"];
            $title=$_POST["title"];
            $tags=$_POST["tags"];
            $color=$_POST["color"];
            $html=$_POST['html'];
            $this->load->model("Context");
            echo $this->Context->editContext($cid,$title,$tags,$color,$html);
        }

}